import { ElMessage } from "element-plus"
const baseUrl='http://jacklv.cn'
interface MyOptions extends Omit<RequestInit,'body'>{
    body?:Record<string,any>
 }
export default function request(url:string,options:MyOptions={}):Promise<any>{
    const {body,...rest} = options;
    let fetchOptions:RequestInit = {
        ...rest
    }
    if(options.method && options.method.toLowerCase() != 'get'){
        fetchOptions.body=JSON.stringify(options.body || {})
        options.headers={
            "Content-Type":"application/json"
        }
    }
    fetchOptions.headers={
         ...(options.headers || {}),
         token:localStorage.token
    }
  
    return fetch(baseUrl+url,fetchOptions)
    .then(response=>response.json())
    .then(res=>{
        if(res.code===1){
            console.log('取回的数据',res.data);
            return res.data
            
        }else{
            ElMessage.error(res.message||'接口有误')
            return Promise.reject(res)
        }
    })


}